package com.yjkf.zh.admin.service.impl;

import com.yjkf.zh.admin.dao.SysLogMapper;
import com.yjkf.zh.admin.model.SysLog;
import com.yjkf.zh.admin.security.SecurityUtils;
import com.yjkf.zh.admin.service.SysLogService;
import com.yjkf.zh.core.page.MybatisPageHelper;
import com.yjkf.zh.core.page.PageRequest;
import com.yjkf.zh.core.page.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysLogServiceImpl implements SysLogService {

    @Autowired
    private SysLogMapper sysLogMapper;

    @Override
    public int save(SysLog record) {
        if(record.getId()==null || record.getId()==0){
            record.setCreateTime(new Date());
            return sysLogMapper.insertSelective(record);

        }
        return sysLogMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int delete(SysLog record) {
        return sysLogMapper.deleteLogById(record.getId());
    }

    @Override
    public int delete(List<SysLog> records) {
        for(SysLog sysLog:records){
            delete(sysLog);
        }
        return 0;
    }

    @Override
    public SysLog findById(Long id) {
        return null;
    }

    @Override
    public PageResult findPage(PageRequest pageRequest) {
      //
        System.out.println(pageRequest.getParam("startTime"));
        String label = SecurityUtils.getUsername();

        Map<Object, Object> params = new HashMap<>();
        params.put("label",label);
        pageRequest.setParams(params);
        Object userName = pageRequest.getParam("label");

      //  if(label != null){
            return MybatisPageHelper.findPage(pageRequest,sysLogMapper,"findPageByUserName",userName);
      //  }
       // return MybatisPageHelper.findPage(pageRequest, sysLogMapper);
    }
}
